//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BlackVolTermStructure.h"
using namespace Cephei::QL::Termstructures::Volatility::Equityfx;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/VolatilityTermStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
#define HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (boost::shared_ptr<QuantLib::BlackVolTermStructure>& childNative, Object^ owner) : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
#ifdef HANDLE
	_phBlackVolTermStructure = NULL;
#endif
	_ppBlackVolTermStructure = &childNative;
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackVolTermStructure));
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (QuantLib::BlackVolTermStructure& childNative, Object^ owner) : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
#ifdef HANDLE
	_phBlackVolTermStructure = NULL;
#endif
	_ppBlackVolTermStructure = new boost::shared_ptr<QuantLib::BlackVolTermStructure> (&childNative);
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackVolTermStructure));
    _BlackVolTermStructureOwner = owner;
    _VolatilityTermStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (CBlackVolTermStructure^ copy) : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
#ifdef HANDLE
	_phBlackVolTermStructure = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBlackVolTermStructure = new boost::shared_ptr<QuantLib::BlackVolTermStructure> (copy->GetShared());
        _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackVolTermStructure));
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (PLATFORM::Type^ t) : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
#ifdef HANDLE
	_phBlackVolTermStructure = NULL;
#endif
	if (!t->IsSubclassOf(CBlackVolTermStructure::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (QuantLib::Handle<QuantLib::BlackVolTermStructure>& childNative, Object^ owner)  : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
	_phBlackVolTermStructure = &childNative;
	_ppBlackVolTermStructure = &static_cast<boost::shared_ptr<QuantLib::BlackVolTermStructure>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackVolTermStructure));
    _BlackVolTermStructureOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (QuantLib::Handle<QuantLib::BlackVolTermStructure> childNative)  : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
	_phBlackVolTermStructure = &childNative;
	_ppBlackVolTermStructure = &static_cast<boost::shared_ptr<QuantLib::BlackVolTermStructure>>(childNative.currentLink());
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackVolTermStructure));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::CBlackVolTermStructure (QuantLib::BlackVolTermStructure childNative)  : CVolatilityTermStructure(CBlackVolTermStructure::typeid)
{
#ifdef HANDLE
	_phBlackVolTermStructure = NULL;
#endif
	_ppBlackVolTermStructure = new boost::shared_ptr<QuantLib::BlackVolTermStructure> (new QuantLib::BlackVolTermStructure (childNative));
    _ppVolatilityTermStructure = new boost::shared_ptr<QuantLib::VolatilityTermStructure> (boost::dynamic_pointer_cast<QuantLib::VolatilityTermStructure> (*_ppBlackVolTermStructure));
}
#endif

Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::~CBlackVolTermStructure ()
{
    if (_ppBlackVolTermStructure != NULL)
    {
	    delete _ppBlackVolTermStructure;
        _ppBlackVolTermStructure = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::!CBlackVolTermStructure ()
{
    if (_ppBlackVolTermStructure != NULL)
    {
	    delete _ppBlackVolTermStructure;
    }
}
QuantLib::BlackVolTermStructure& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::GetReference ()
{
    if (_ppBlackVolTermStructure == NULL) throw REFNEW NativeNullException ();
	return **_ppBlackVolTermStructure;
}
boost::shared_ptr<QuantLib::BlackVolTermStructure>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::GetShared ()
{
    if (_ppBlackVolTermStructure == NULL) throw REFNEW NativeNullException ();
	return *_ppBlackVolTermStructure;
}
QuantLib::BlackVolTermStructure* Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::GetPointer ()
{
    if (_ppBlackVolTermStructure == NULL) throw REFNEW NativeNullException ();
	return &**_ppBlackVolTermStructure;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BlackVolTermStructure>& Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::GetHandle ()
{
	if (_phBlackVolTermStructure == NULL)
	{
		_phBlackVolTermStructure = new Handle<QuantLib::BlackVolTermStructure> (*_ppBlackVolTermStructure);
	}
	return *_phBlackVolTermStructure;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::HasNative () 
{
	return (_ppBlackVolTermStructure != NULL);
}

Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackForwardVariance (Double time1, Double time2, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _time1 = (QuantLib::Time)ValueHelper::Convert (time1); //a
        QuantLib::Time _time2 = (QuantLib::Time)ValueHelper::Convert (time2); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVolTermStructure)->blackForwardVariance ( _time1,  _time2,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackForwardVariance (DateTime date1, DateTime date2, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _date1 = (QuantLib::Date)ValueHelper::Convert (date1); //a
        QuantLib::Date _date2 = (QuantLib::Date)ValueHelper::Convert (date2); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVolTermStructure)->blackForwardVariance ( _date1,  _date2,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackForwardVol (Double time1, Double time2, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _time1 = (QuantLib::Time)ValueHelper::Convert (time1); //a
        QuantLib::Time _time2 = (QuantLib::Time)ValueHelper::Convert (time2); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackVolTermStructure)->blackForwardVol ( _time1,  _time2,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackForwardVol (DateTime date1, DateTime date2, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _date1 = (QuantLib::Date)ValueHelper::Convert (date1); //a
        QuantLib::Date _date2 = (QuantLib::Date)ValueHelper::Convert (date2); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackVolTermStructure)->blackForwardVol ( _date1,  _date2,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackVariance (Double maturity, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVolTermStructure)->blackVariance ( _maturity,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackVariance (DateTime maturity, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppBlackVolTermStructure)->blackVariance ( _maturity,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackVol (Double maturity, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Time _maturity = (QuantLib::Time)ValueHelper::Convert (maturity); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackVolTermStructure)->blackVol ( _maturity,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Equityfx::CBlackVolTermStructure::BlackVol (DateTime maturity, Double strike, Microsoft::FSharp::Core::FSharpOption<Boolean>^ extrapolate)
{
    try
    {
        QuantLib::Date _maturity = (QuantLib::Date)ValueHelper::Convert (maturity); //a
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike); //a
        bool _extrapolate = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (extrapolate) ? (bool)ValueHelper::Convert (extrapolate->Value) : false); //9a
    	QuantLib::Volatility _rv = (QuantLib::Volatility)(*_ppBlackVolTermStructure)->blackVol ( _maturity,  _strike,  _extrapolate );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

